package com.whf.android.jar.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.DisplayCutout;
import android.view.View;
import android.view.WindowInsets;

import java.lang.reflect.Method;
import java.util.List;

/**
 * 流海屏 判断
 *
 * @author qf
 * @date 2020-01-12
 */
public class FlowingSeaUtil {

    /**
     * android P 是否流海屏
     *
     * @param act:Activity
     * @return true
     */
    public static boolean hasNotchP(Activity act) {
        if (Build.VERSION.SDK_INT >= 28) {
            View decorView = act.getWindow().getDecorView();
            WindowInsets rootWindowInsets = decorView.getRootWindowInsets();
            if (rootWindowInsets != null) {
                DisplayCutout displayCutout = rootWindowInsets.getDisplayCutout();
                if (displayCutout != null) {
                    List<Rect> boundingRects = displayCutout.getBoundingRects();
                    return !boundingRects.isEmpty();
                }
            }
            return false;
        } else {
            return false;
        }
    }

    /**
     * 华为是否有流海屏
     *
     * @param context context
     * @return 是否有流海屏
     */
    public static boolean hasNotchAtHuawei(Context context) {
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            ret = (boolean) get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException e) {
            Log.e("Notch", "hasNotchAtHuawei ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("Notch", "hasNotchAtHuawei NoSuchMethodException");
        } catch (Exception e) {
            Log.e("Notch", "hasNotchAtHuawei Exception");
        } finally {
            return ret;
        }
    }

    /**
     * 小米是否流海屏
     *
     * @param act
     * @return
     */
    public static boolean hasNotchAtXiaoMi(Activity act) {
        String s = SystemPropertiesProxy.get(act, "ro.miui.notch", "0");
        if (s.equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * oppo是否有流海屏
     *
     * @param context context
     * @return 是否有流海屏
     */
    public static boolean hasNotchAtOPPO(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    /**
     * vivo是否有流海屏
     *
     * @param context context
     * @return 是否有流海屏
     */
    public static boolean hasNotchAtVivo(Context context) {
        final int VIVO_NOTCH = 0x00000020;//是否有刘海
        final int VIVO_FILLET = 0x00000008;//是否有圆角
        boolean ret = false;
        try {
            ClassLoader classLoader = context.getClassLoader();
            Class FtFeature = classLoader.loadClass("android.util.FtFeature");
            Method method = FtFeature.getMethod("isFeatureSupport", int.class);
            ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
        } catch (ClassNotFoundException e) {
            Log.e("Notch", "hasNotchAtVivo ClassNotFoundException");
        } catch (NoSuchMethodException e) {
            Log.e("Notch", "hasNotchAtVivo NoSuchMethodException");
        } catch (Exception e) {
            Log.e("Notch", "hasNotchAtVivo Exception");
        } finally {
            return ret;
        }
    }
}
